﻿using System.Web.Mvc;

namespace ComeSeeThis_MVC2.Models
{
    public class GearingRecSearchForm
    {
        public GearingRec GearingRec { get; private set; }
        public SelectList Vehicles { get; private set; }
        public SelectList VehicleTypes { get; private set; }
        public SelectList Motors { get; private set; }
        //public SelectList Turns { get; private set; }
        //public SelectList KiloVolt { get; private set; }
        public SelectList TrackSize { get; private set; }
        public SelectList TrackSurface { get; private set; }
        public SelectList CellTypes { get; private set; }
        public string VehicleTypeScript { get; private set; }
        public string TransRatioScript { get; private set; }
        //public float TransmisstionRatio { get; private set; }

        public GearingRecSearchForm()
        {
            
        }
        
        public GearingRecSearchForm(GearingRec gearingRec)
        {
            GearingRec = gearingRec;
            var gearingRecReposiory = new GearingRecRepository();
            
            #region Vehicles

            var vehicleList = new VehicleRepository().GetAllVehicles();

            VehicleTypeScript = "VehicleTypes = new Array(";
            TransRatioScript = "TransRatios = new Array(";
            var first = true;

            foreach (var vehicle in vehicleList)
            {
                //if (gearingRec. Vehicle == null) Vehicle = new VehicleRepository().GetVehicle(vehicle.VehicleId);
                vehicle.VehicleName = vehicle.Company.CompanyName + " " + vehicle.VehicleName;

                if (!first)
                {
                    VehicleTypeScript += ", ";
                    TransRatioScript += ", ";
                }
                first = false;
                VehicleTypeScript += vehicle.VehicleTypeId;
                TransRatioScript += vehicle.TransmissionRatio;
            }
            VehicleTypeScript += ");";
            TransRatioScript += ");";

            Vehicles = new SelectList(vehicleList, "VehicleId", "VehicleName", null);

            #endregion

            #region Vehicle Types

            var vehicleTypeList = new VehicleTypeRepository().GetAllVehicleTypes();
            foreach (var vehicleType in vehicleTypeList)
            {
                vehicleType.VehicleTypeName = "1/" + vehicleType.Scale + " " + vehicleType.VehicleTypeName;
            }
            VehicleTypes = new SelectList(vehicleTypeList, "VehicleTypeId", "VehicleTypeName", gearingRec.VehicleTypeId);

            #endregion

            #region Motors
            
            var motorList = new MotorRepository().GetAllMotors();
            foreach (var motor in motorList)
            {
                motor.MotorName = motor.Company.CompanyName + " " + motor.MotorName;
            }
            Motors = new SelectList(motorList, "MotorId", "MotorName");

            #endregion

            #region Cells, Track Size and Surface

            CellTypes = new SelectList(gearingRecReposiory.GetAllCellTypes(), "CellTypeId", "CellTypeName", 4);
            TrackSize = new SelectList(gearingRecReposiory.GetAllTrackSizes(), "TrackSizeId", "TrackSizeName");
            TrackSurface = new SelectList(gearingRecReposiory.GetAllTrackSurfaces(), "TrackSurfaceId", "TrackSurfaceName");

            #endregion
        }
    }
}